<template>
	<view>
		<teacher v-if="flag == 'teacher'"></teacher>
		<student v-else></student>
	</view>
</template>

<script>
import { Base64 } from 'js-base64';
import student from '../components/student.vue'
import teacher from '../components/teacher.vue'
export default {
	components: {
		student,
		teacher
	},
	data() {
		return {
			flag: '',
			tabList: []
		}
	},
	onLoad() {
		this.flag = uni.getStorageSync('flag');

		// // 获取当前页面的URL
		// const url = window.location.href;
		// // 使用URLSearchParams解析URL中的参数
		// const params = new URLSearchParams(new URL(url).search);
		// // 删除token参数
		// params.delete('token');
		// if (this.flag == 'student') {
		// 	params.delete('name');
		// }
		// // 重新构建URL
		// const newUrl = `${new URL(url).origin}/${new URL(url).hash}`;
		// // 重新跳转到新的URL
		// if (url !== newUrl) {
		// 	window.location.href = newUrl;
		// }
	},
	onShow() {
		if (this.flag == 'student') {
			this.$tools.axiosFromToken('POST', 'prouser/getById', {}, '加载中').then(res => {
				if (res.code != 200) return this.$tools.showtt(res.msg)
				uni.setStorageSync('getInfo', JSON.parse(Base64.decode(res.msg)))
			})

			const unreportList = [];

			this.$tools.axiosFromToken('GET', 'project/practice/type/config/listAll', {}, '加载中').then(res => {
				if (res.code != 200) return this.$tools.showtt(res.msg)

				res.rows.forEach(item => {
					this.$tools.axiosFromToken('GET', 'practice/user/popUps', { practiceType: item.id }, '加载中').then(res1 => {
						if (res1.code != 200) return this.$tools.showtt(res1.msg)
						unreportList.push({ practice_type: item.id, number: res1.data.haveApplyButNoDoNumber + res1.data.toBeFilledInNumber });
					})
				});

				this.$tools.axiosFromToken('GET', 'practice/user/studentUnreported', {}, '加载中').then(res2 => {
					if (res2.code != 200) return this.$tools.showtt(res2.msg)
					if (res2.data.find(item => item.practice_type == 99998)) {
						unreportList.push({ ...res2.data.find(item => item.practice_type == 99998) });
					}
					if (res2.data.find(item => item.practice_type == 99999)) {
						unreportList.push({ ...res2.data.find(item => item.practice_type == 99999) });
					}

					uni.removeStorageSync('studentUnreport');
					uni.setStorageSync('studentUnreport', unreportList);
					uni.$emit('studentUnreportEmit', unreportList);
				})
			})
		} else {
			this.$tools.axiosFromToken('POST', 'sysUser/getById', {}, '加载中').then(res => {
				let getAllInfo = { ...res.data, user: JSON.parse(Base64.decode(res.data.user)) }
				if (res.code != 200) return this.$tools.showtt(res.msg)
				uni.setStorageSync('getInfo', getAllInfo);
				uni.removeStorageSync('sortedTabList');
				this.getRouter();
			})
		}
	},
	mounted() {
	},
	methods: {
		getRouter() {
			const auditList = uni.getStorageSync('getInfo').routes.find(v => v.name === "DistributeTasks");
			let tabList = [];
			if (auditList && auditList.children.length > 0 && auditList.children.find(v => v.name === "TaskReview")) {
				const auditChildrenList = auditList.children.find(v => v.name === "TaskReview");
				if (auditChildrenList) {
					const appliceChild = auditChildrenList.children.find(v => v.name === "ApplicaAudits");
					const hospitalChild = auditChildrenList.children.find(v => v.name === "Hospital");
					if (appliceChild) {
						tabList.push({
							name: '报名审核',
							img: '/static/yuanji.png',
							path: '/teacher/examine/applicaAudits'
						});
					}

					if (hospitalChild) {
						tabList.push({
							name: '报告审核',
							img: '/static/xiaoji.png',
							path: '/teacher/examine/collegeAudit'
						});
					}

					uni.setStorageSync('sortedTabList', tabList);
					uni.$emit('sortedTabListUpdated', tabList);  // 使用事件总线通知子页面数据已更新
				}
			} else {
				uni.$emit('sortedTabListUpdated', []);
			}
		},
	},
}
</script>

<style lang="scss"></style>
